<?php
require_once '../config.php';
$filename = PATH_CONFIG.'list_controller.json';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#"
	dir="ltr" lang="en-US">
<head>
<title>Quản lý controller</title>
<style type="text/css">
table {
	border: 1px solid #555;
	width: 100%;
}

table th{
	background:#ccc;
	height:30px;
}

table td {
	border: 1px solid #555;
}

table tr:nth-child(odd) {
	background:#ccc
}

table th {
	border: 1px solid #555;
}
</style>
</head>
<body>
<h2>Quản lý controller</h2>
<?php
$data = read_config($filename);
if(isset($_POST['title']) && isset($_POST['path'])){
	$data = array_merge($data,array($_POST['title'] => $_POST['path']));
	write_config($filename, $data);
}

if(isset($_GET['del'])){
	unset($data[$_GET['del']]);
	write_config($filename, $data);
}

if(isset($_GET['edit'])){
	$title = $_GET['edit'];
	$path = $data[$_GET['edit']];
	write_config($filename, $data);
}

if(isset($_POST['edit']) && !empty($_POST['edit']) && isset($_POST['path'])){
	$data[$_POST['edit']] = $_POST['path'];
	write_config($filename, $data);
}
?>
<?php echo isset($_GET['edit']) ? '<a href="add_controller.php"><< Quay lại</a>' : '';?>
<form action="add_controller.php" method="post">
<p>Tên controller: <input type="text" id="title" <?php echo isset($_GET['edit']) ? 'readonly="readonly"' : '';?> name="title"
	value="<?php echo isset($title) ? $title : '';?>" /> Đường dẫn: <input
	type="text" id="path" name="path"
	value="<?php echo isset($path) ? $path : '';?>" /> <input type="hidden"
	name="edit"
	value="<?php echo isset($_GET['edit']) ? $_GET['edit'] : '';?>" /> <input
	type="submit" name="submit" value="Lưu" id="submit" /></p>
<?php
if(is_array($data)):
?>
<table>
	<tr>
		<th>Tên controller</th>
		<th>Đường dẫn</th>
		<th>Xóa</th>
	</tr>
	<?php foreach ($data as $k => $v):?>
	<tr>
		<td><a href="?edit=<?php echo $k;?>"><?php echo $k;?></a></td>
		<td><?php echo $v;?></td>
		<td width="50" align="center"><a href="?del=<?php echo $k;?>">Xóa</a></td>
	</tr>
	<?php endforeach;?>
</table>
	<?php endif;?></form>
</body>
</html>
